/**
 * Created by chris on 16/9/27.
 */

angular.module('starter.controllers', [])
.controller('HomeController', function ($scope) {
    
})
.controller('HotsController', function ($scope, HotsFactory, G) {

    $scope.imgurl = G.imgUrl;

    //下拉刷新
    $scope.refreshHot = function () {
        //获取该标题的catid ...
        HotsFactory.getHots(15);
    }

    //监听, 一旦收到broadcast, 就去取数据
    $scope.$on('updateHots', function () {
        $scope.hotsData = HotsFactory.getHotsData();
        console.log($scope.hotsData);
        //结束刷新
        $scope.$broadcast('scroll.refreshComplete');
    });

    //发起请求
    //当某个操作非常耗时的时候,需要用异步请求
    //异步请求: 当程序执行到这一行,会直接往下执行, 当请求完成时回调相关的方法
    HotsFactory.getHots(15);
    
    $scope.selectPage = function (page, catid) {
        //改变ui效果
        var btns = $('.sub_header_list a');
        $.each(btns, function (index, ele) {
            ele.className = 'button button-clear';
        });
        btns[page].className = 'button button-clear button-select';
        //发起请求
        HotsFactory.getHots(catid);
    }


})
    .controller('HotsDetailController', function ($scope, $rootScope, HotsDetailFactory, $stateParams) {

        //进入详情页, 隐藏tabs
        $rootScope.hideTabs = 'tabs-item-hide';

        //当退出时,页面会销毁释放
        //监听销毁事件
        $scope.$on('$destroy', function () {
            $rootScope.hideTabs = '';
        })

        //页面加载之前
        $scope.$on('$ionicView.beforeEnter', function () {
            console.log('before enter');
        });
        //页面加载完成
        $scope.$on('$ionicView.afterEnter', function () {
            console.log('after enter');
        });

        //先监听, 再请求
        $scope.$on('updateHotsDetail', function () {
            $scope.hotsDetailData = HotsDetailFactory.getHotsDetailData();
            // console.log($scope.hotsDetailData);
            $scope.showLoading = false;
        });

        //$stateParams 用来获取传递过来的参数
        console.log($stateParams.aid);
        //显示加载指示器
        $scope.showLoading = true;
        HotsDetailFactory.getHotsDetail($stateParams.aid);


    })
.controller('ArticleController', function ($scope) {
    
})
.controller('MineController', function ($scope, Storage) {
    $scope.userInfo = Storage.get('user');
})
.controller('LoginController', function ($scope, $state, $ionicLoading, User, Storage) {

    $scope.user = {
        username: '',
        password: ''
    }
    
    $scope.$on('loginUpdate', function () {

        var user = User.getCurrentUser();
        if (user.success == true){
            //登录成功 则将用户信息储存在本地
            Storage.set('user', $scope.user);
            //1. 跳转之后刷新前一个页面   2. 根据需求改变样式
            $state.go('tab.mine', {}, {reload: true});
        }else {
            $ionicLoading.show({
                template: user.message,
                duration: 1500
            });
        }
    })

    $scope.signIn = function () {
        console.log($scope.user.username);
        User.login($scope.user.username, $scope.user.password);
    }
})




